package com.example.myhelloworld.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import java.util.Vector;

public class TextExercise extends ExerciseClass {

	String mode;
	DockPanel textproblem = new DockPanel();
	TPInterfaceAsync textProblemSet = (TPInterfaceAsync) GWT.create(TPInterface.class);
	TextProblem text;
	int resultPoints = 100;

	@SuppressWarnings("deprecation")
	public TextExercise(String uName, String settings,
			GreetingServiceAsync GSA) {
		super(uName, settings, GSA);
		this.selfPanel = textproblem; 
		userName = uName;
		gsa = GSA;
		mode = settings;
		produceResultButton = new Button("Proceed");
		//		final TextProblem text = new TextProblem("What is x1 + x2?", "x1 + x2", "1 20 1 20", "Simply add x1 and x2 together.");

		textProblemSet.getRandomProblem(new AsyncCallback<Vector<String>>() {
			public void onSuccess(Vector<String> result) {
				text = new TextProblem(result.get(0), result.get(1), result.get(2), result.get(3));
				textproblem.add(new HTML(text.getProblemText()), DockPanel.NORTH);
			}
			public void onFailure(Throwable caught) {
				System.out.println("TPSet.getRandomProblem failed: "+caught.getMessage());
			}  
		});

		final TextBox textbox = new TextBox();

		textproblem.add(textbox, DockPanel.CENTER);
		textproblem.setCellHorizontalAlignment(textbox, DockPanel.ALIGN_CENTER);
		textproblem.addStyleName("Exercise");
		//		textproblem.setSize("200px", "200px");

		textbox.addKeyboardListener((new KeyboardListener(){

			public void onKeyDown(Widget sender, char keyCode, int modifiers) {
				
			}

			public void onKeyPress(Widget sender, char keyCode, int modifiers) {
				// TODO Auto-generated method stub
				if (keyCode == KeyCodes.KEY_ENTER) {
					//TODO: something more must happen when the answer is correct or incorrect
					if (textbox.getText().equals(text.getAnswer())){

						System.out.println("Correct Answer!");
						
						textproblem.add(produceResultButton,DockPanel.SOUTH);
						textproblem.add(new HTML("Good Job!"), DockPanel.SOUTH);

					} else {
						textproblem.add(new HTML(text.getComment()), DockPanel.SOUTH);
						System.out.println(textbox.getText());
						resultPoints = (int)(resultPoints*0.5);	//you get less points if you answer incorrectly and receives hints.
					}
				}
			}

			public void onKeyUp(Widget sender, char keyCode, int modifiers) {
				// TODO Auto-generated method stub

			}}));
	}

	@Override
	public void draw() {
	}

	@Override
	public String pushresult() {
		return Integer.toString(resultPoints);
	}

}
